<template>
    <div>
        <Row>
            <Card class="sys-user">
                <p slot="title">
                    <Icon type="ios-list"></Icon>
                    用户编辑
                </p>
                <Form ref="formItem" :model="formItem" :rules="ruleItem" :label-width="80">
                    <FormItem label="登录名" prop="login_name">
                        <Input v-model="formItem.login_name" placeholder="登录名" />
                    </FormItem>
                    <FormItem label="密码" prop="password">
                        <Input v-model="formItem.password" placeholder="密码" />
                    </FormItem>
                    <FormItem>
                        <Button type="primary" @click="handleSubmit('formItem')">提交</Button>
                        <Button type="ghost" style="margin-left: 8px" @click="backPage">返回</Button>
                    </FormItem>
                </Form>
            </Card>
        </Row>
    </div>
</template>

<script>
    import util from '@/libs/util.js';
    import urls from '@/store/modules/urls.js';

    export default {
        name: 'edit',
        data () {
            return {
                formItem: {
                    login_name: '',
                    password: ''
                },
                ruleItem: {
                    login_name: [
                        { required: true, message: '请输入登录名', trigger: 'blur' }
                    ],
                    password: [
                        { required: true, message: '请输入密码', trigger: 'blur' },
                        { type: 'string', min: 6, message: '密码长度不能低于6位数', trigger: 'blur' }
                    ]
                }
            };
        },
        created () {
            let params = this.$route.params;
            util.ajax.get(urls.sysUserGet + util.urlConcat(params)).then((response) => {
                let data = response.data;
                if (!data) {
                    this.$Message.error('请求失败!');
                } else if (data.code === 200) {
                    this.formItem = data.data;
                } else {
                    this.$Message.error(data.message);
                }
            });
        },
        methods: {
            backPage () {
                this.$router.go(-1);
            },
            handleSubmit (name) {
                this.$refs[name].validate((valid) => {
                    if (valid) {
                        let data = util.dataConcat(this.formItem, 'sysUser');
                        util.ajax.post(urls.sysUserSave, data, {
                            headers: {
                                'Content-Type': 'application/x-www-form-urlencoded'
                            }
                        }).then((response) => {
                            let data = response.data;
                            if (!data) {
                                this.$Message.error('请求失败!');
                            } else if (data.code === 200) {
                                this.$Message.success(data.message);
                            } else {
                                this.$Message.error(data.message);
                            }
                        });
                    }
                });
            }
        }
    };
</script>
